using System;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Package;
using MH.WsdlWorks.ProjectModel;

namespace MH.WsdlWorks
{
    [CLSCompliant(false)]
    [ComVisible(true)]
    [Guid("D0499C2E-25A9-4a60-9305-A0100985965C")]
    public class WsdlNode : NodeBase
    {
        // type guid of a WSDL node
        Guid typeGuid = new Guid("D37244EB-BB01-43e0-A94C-878F7A407E56");
        Wsdl wsdl;

        public Wsdl Wsdl
        {
            get { return wsdl; }
            set { wsdl = value; }
        }

        public WsdlNode(SimpleProjectNode root, Wsdl wsdl) 
            : base(root, wsdl.GetPath())
        {
            this.wsdl = wsdl;
            foreach (MessageFile wsdlFile in wsdl.WsdlFiles)
            {
                AddWsdlFileNodeToProject(wsdlFile);
            }
            foreach (Operation operation in wsdl.Operations)
            {
                AddOperationNodeToProject(operation);
            }
        }

        private void AddWsdlFileNodeToProject(MessageFile messageFile)
        {
            string itemType = "Content"; // this is the compile directive
            SimpleProjectElement projectElement = ((SimpleProjectNode)this.ProjectMgr).CreateMsBuildFileItem(
                messageFile.GetPath(), itemType);
            MessageFileNode wsdlFileNode = new MessageFileNode(this.ProjectMgr, projectElement, messageFile);
            this.AddChild(wsdlFileNode);
        }

        public void AddOperationNodeToProject(Operation operation)
        {
            OperationNode operationNode = new OperationNode(this.ProjectMgr, operation);
            this.AddChild(operationNode);
        }

        protected override SimpleNodeProperties CreatePropertiesObject()
        {
            return new WsdlNodeProperties(this);
        }

        public override object GetIconHandle(bool open)
        {
            // TODO: create and return a wsdl node icon
            return this.ProjectMgr.ImageHandler.GetIconHandle(open ? (int)ProjectNode.ImageName.OpenFolder : (int)ProjectNode.ImageName.Folder);
        }

        public override Guid ItemTypeGuid
        {
            get { return typeGuid; }
        }

        protected override int QueryStatusOnNode(Guid guidCmdGroup, uint cmd, IntPtr pCmdText, ref QueryStatusResult result)
        {
            if (guidCmdGroup == GuidList.guidTestMenuPackageCmdSet)
            {
                return VSConstants.S_OK;
            }
            return base.QueryStatusOnNode(guidCmdGroup, cmd, pCmdText, ref result);
        }

        public override int MenuCommandId
        {
            get
            {
                return MenuIdList.menuIdWsdlNode;
            }
        }

        public override int SortPriority
        {
            get
            {
                return DefaultSortOrderNode.FolderNode;
            }
        }
    }
}
